package com.csu.ui;
import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;

import com.csu.income.IncomePanel;
import com.csu.outlay.OutlayPanel;

public class MainUI extends JFrame {

	//用户类型
	private int userType;
	//用户名
	private String currAcct;


	//子界面
//	private HomePanel homePanel = new HomePanel();
	private HomePanel homePanel;
	private IncomePanel incomePanel = new IncomePanel();
	private OutlayPanel outlayPanel = new OutlayPanel();
	private UserPanel userPanel = new UserPanel();

	public String getCurrAcct() {
		return currAcct;
	}

	public void setCurrAcct(String currAcct) {
		this.currAcct = currAcct;
	}

	public int getUserType() {
		return userType;
	}

	public void setUserType(int usertype) {
		this.userType = usertype;
	}

	//显示主页
	public void homePage() {
		homePanel.setVisible(true);
		incomePanel.setVisible(false);
		outlayPanel.setVisible(false);
		userPanel.setVisible(false);
	}

	MainUI(){

		this.setTitle("家庭财务管理系统");
		this.setSize(1250,860);
		this.setResizable(false);
		this.setLocationRelativeTo(null);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.getContentPane().setBackground(new Color(0,162,232));

		this.setLayout(null);
		//主页按钮
		JButton homeBtn = new JButton("主页");
		homeBtn.setBounds(25,50,100,50);
		homeBtn.setContentAreaFilled(false);
		homeBtn.setBorder(null);
		homeBtn.setFont(new Font("黑体",Font.BOLD,25));
		homeBtn.setForeground(Color.WHITE);
		this.add(homeBtn);
		//收入按钮
		JButton incomeBtn = new JButton("收入");
		incomeBtn.setBounds(25,150,100,50);
		incomeBtn.setContentAreaFilled(false);
		incomeBtn.setBorder(null);
		incomeBtn.setFont(new Font("黑体",Font.BOLD,25));
		incomeBtn.setForeground(Color.WHITE);
		this.add(incomeBtn);
		//支出按钮
		JButton payBtn = new JButton("支出");
		payBtn.setBounds(25,250,100,50);
		payBtn.setContentAreaFilled(false);
		payBtn.setBorder(null);
		payBtn.setFont(new Font("黑体",Font.BOLD,25));
		payBtn.setForeground(Color.WHITE);
		this.add(payBtn);
		//用户按钮
		JButton userBtn = new JButton("用户");
		userBtn.setBounds(25,350,100,50);
		userBtn.setContentAreaFilled(false);
		userBtn.setBorder(null);
		userBtn.setFont(new Font("黑体",Font.BOLD,25));
		userBtn.setForeground(Color.WHITE);
		this.add(userBtn);
		//注销按钮
		JButton offBtn = new JButton("注销");
		offBtn.setBounds(25,650,100,50);
		offBtn.setContentAreaFilled(false);
		offBtn.setBorder(null);
		offBtn.setFont(new Font("黑体",Font.BOLD,25));
		offBtn.setForeground(Color.WHITE);
		this.add(offBtn);

		//初始化子窗口
		homePanel = new HomePanel();
		homePanel.setBounds(150,0,1110,860);
		this.add(homePanel);
		homePanel.setVisible(true);

		incomePanel.initData();
		incomePanel.initPage();
		incomePanel.initButton();
		incomePanel.setBounds(150,0,1110,860);
		this.add(incomePanel);
		incomePanel.setVisible(false);

		outlayPanel.initData();
		outlayPanel.initPage();
		outlayPanel.initNew();
		outlayPanel.setBounds(150,0,1110,860);
		this.add(outlayPanel);
		outlayPanel.setVisible(false);

		userPanel.setBounds(150,0,1110,860);
		this.add(userPanel);
		userPanel.setVisible(false);

		//按钮事件
		homeBtn.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {


				homePanel = new HomePanel();
				homePanel.setBounds(150,0,1110,860);
				LoginUI.mainUI.add(homePanel);

				homePanel.setVisible(true);
				incomePanel.setVisible(false);
				outlayPanel.setVisible(false);
				userPanel.setVisible(false);
			}
		});

		incomeBtn.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				showIncome();
//				homePanel.setVisible(false);
//				incomePanel.setVisible(true);
//				outlayPanel.setVisible(false);
//				userPanel.setVisible(false);
			}
		});

		payBtn.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				showPay();
//				homePanel.setVisible(false);
//				incomePanel.setVisible(false);
//				outlayPanel.setVisible(true);
//				userPanel.setVisible(false);
			}
		});

		userBtn.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				showUser();
//				userPanel.setAllFalse();
//				userPanel.setLabelText("您好，"+currAcct+"！");
//				userPanel.setCurrAcct(currAcct);
//				if(userType==1) {
//					userPanel.setBtnUnable();
//				}
//				else {
//					userPanel.setBtnAble();
//				}
//				homePanel.setVisible(false);
//				incomePanel.setVisible(false);
//				outlayPanel.setVisible(false);
//				userPanel.setVisible(true);
			}
		});

		offBtn.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				if(JOptionPane.showConfirmDialog(null,"是否注销","提示",JOptionPane.YES_NO_OPTION)==0) {
					LoginUI.main(null);
				}
			}
		});

	}

	public void showIncome() {
		homePanel.setVisible(false);
		incomePanel.setVisible(true);
		outlayPanel.setVisible(false);
		userPanel.setVisible(false);
	}

	public void showPay() {
		homePanel.setVisible(false);
		incomePanel.setVisible(false);
		outlayPanel.setVisible(true);
		userPanel.setVisible(false);
	}

	public void showUser() {
		userPanel.setAllFalse();
		userPanel.setLabelText("您好，"+currAcct+"！");
		userPanel.setCurrAcct(currAcct);
		if(userType==1) {
			userPanel.setBtnUnable();
		}
		else {
			userPanel.setBtnAble();
		}
		homePanel.setVisible(false);
		incomePanel.setVisible(false);
		outlayPanel.setVisible(false);
		userPanel.setVisible(true);
	}

}
